// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"name": {
		"rules": [{
				"required": true
			},
			{
				"format": "string"
			}
		],
		"title": "剧名",
		"label": "剧名"
	},
	"imgUrl": {
		"rules": [{
				"required": true
			},
			{
				"format": "string"
			}
		],
		"title": "封面图片",
		"label": "封面图片"
	},
	"order": {
		"rules": [{
				"required": true
			},
			{
				"format": "int"
			}
		],
		"title": "排序",
		"label": "排序"
	},
	"status": {
		"rules": [{
			"format": "string"
		}],
		"title": "状态",
		"label": "状态"
	},
	"toStar": {
		"rules": [{
			"format": "string"
		}],
		"title": "演员",
		"label": "演员"
	},
	"toStar": {
		"rules": [{
			"format": "string"
		}],
		"title": "演员",
		"label": "演员"
	},
	"year": {
		"rules": [{
			"format": "string"
		}],
		"title": "年份",
		"label": "年份"
	},
	"synopsis": {
		"rules": [{
			"format": "string"
		}],
		"title": "简介",
		"label": "简介"
	},
	"type": {
		"rules": [{
			"format": "string"
		}],
		"title": "类型",
		"label": "类型"
	},
	"table": {
		"rules": [{
				"required": true
			},
			{
				"format": "string"
			},
			{
				"range": [{
						"value": "video",
						"text": "电视剧"
					},
					{
						"value": "film-video",
						"text": "电影"
					},
					{
						"value": "comic-video",
						"text": "动漫"
					},
					{
						"value": "variety-video",
						"text": "综艺"
					}
				]
			}
		],
		"title": "类别",
		"label": "类别"
	}
}

const enumConverter = {
	"table_valuetotext": {
		"video": "电视剧",
		"film-video": "电影",
		"comic-video": "动漫",
		"variety-video": "综艺"
	}
}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}